package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOwnerOrdertype;
import com.jy.wms.pojo.WmsRejected;
import com.jy.wms.service.WmsRejectedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsRejectedController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Jan 16 17:18:20 CST 2018
 */
@RestController
@RequestMapping("/wmsRejected")
@Api(value = "/wmsRejected", description = "退货")
public class  WmsRejectedController extends BaseController<WmsRejectedService,WmsRejected> {

    @ApiOperation(value = "退货页面查询列表", response = JSONObject.class, notes = "退货页面查询列表")
    @PostMapping(value = "/list")
    public @ResponseBody JSONObject list(@RequestBody WmsRejected entity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            Page<WmsRejected> list = new Page<WmsRejected>();
            list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "选择发运订单列表", response = JSONObject.class, notes = "查询列表 需要传入page、rows，可选参数sobh,lxr,lxrdh,xxdz,ids")
    @PostMapping(value = "/soList")
    public @ResponseBody JSONObject soList(@RequestBody Map<String,Object> mapPage, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                mapPage.put("hzid",hz.get(0).getHzid());
            }
            mapPage.put("ckid",String.valueOf(userinfo.getUser().getOrgId().get(0)));
           Page<Map<String,Object>> map = baseService.getSoPage(mapPage);
            json = SetStatus.setSearchType(JSONArray.fromObject(map).toString(),(int) map.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "保存退货单", response = JSONObject.class, notes = "保存退货单")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject add(@RequestBody WmsRejected wmsRejected, HttpSession session) {
        try {
            baseService.baseInsert(wmsRejected,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"保存退货单成功","", "");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"保存退货单成功","", "");
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"保存退货单失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"保存退货单失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "保存退货单", response = JSONObject.class, notes = "保存退货单")
    @PostMapping(value = "/getOrderTypeCk")
    public @ResponseBody
    JSONArray getOrderTypeCk(HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        try {
            List<WmsOwnerOrdertype> list =  baseService.getOrderTypeCk(session);
            for (int i = 0; i < list.size(); i++) {
                JSONObject json = new JSONObject();
                json.put("dictionaryid",list.get(i).getId());
                json.put("value",list.get(i).getDdlx());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
}
